package model.collisions2d;

import java.util.HashSet;
import java.util.Set;

import model.ModelObject;

public class Collision {
	private final HashSet<ModelObject> involved = new HashSet<ModelObject>();
	
	public Collision(ModelObject m1,ModelObject m2) throws UniquenessException{
		if(!(involved.add(m1) && involved.add(m2))){
			throw new UniquenessException();
		}
	}
	
	public Set<ModelObject> getInvolvedObjects(){
		return involved;
	}
	
	@Override
	public boolean equals(Object o){
		boolean result = false;
		if(o instanceof Collision){
			Collision col = (Collision)o;
			result = col.getInvolvedObjects().containsAll(involved);
		}
		return result;
	}
	
	@Override
	public int hashCode(){
		int result = 0;
		for(ModelObject o :involved){
			result+=o.hashCode();
		}
		return result;
	}
	
	public boolean isInvolved(ModelObject o){
		return involved.contains(o);
	}
	
	public ModelObject getOther(ModelObject mo){
		ModelObject result = null;
		for(ModelObject m:involved){
			if(!m.equals(mo)){
				result = m;
			}
		}
		return result;
	}
}
